/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Additionalfund;
import com.cashmate.hibernate.pojo.Bankcreditmemo;
import com.cashmate.hibernate.pojo.Bankdebitmemo;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Cashcustodianreport;
import com.cashmate.hibernate.pojo.CashcustodianreportChecks;
import com.cashmate.hibernate.pojo.CashcustodianreportCollections;
import com.cashmate.hibernate.pojo.CashcustodianreportDepositintransit;
import com.cashmate.hibernate.pojo.Cashdisbursement;
import com.cashmate.hibernate.pojo.Checks;
import com.cashmate.hibernate.pojo.Collections;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

public class CashcustodianreportDAO extends DAO {

    public CashcustodianreportDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Cashcustodianreport> getAll() throws Exception {
        List<Cashcustodianreport> a = (List<Cashcustodianreport>) getSession().createQuery("from Cashcustodianreport a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Cashcustodianreport get(int id) throws Exception {
        Cashcustodianreport a = (Cashcustodianreport) getSession().createQuery("from Cashcustodianreport a where a.ccrid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Cashcustodianreport a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Cashcustodianreport a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Cashcustodianreport> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Cashcustodianreport> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    @SuppressWarnings("unchecked")
    public String saveCCR(Cashcustodianreport ccr, List<Checks> checklist, List<Cashdisbursement> cashlist, List<Additionalfund> fundlist, List<Bankdebitmemo> bankdebitlist, List<Bankcreditmemo> bankcreditlist, List<Collections> collectionlist, List<Collections> depositlist) throws Exception {
        try {
            Session ses = getSession();
            begin();
            CashcustodianreportChecks ccrcheck = null;
            CashcustodianreportCollections ccrcoll = null;
            CashcustodianreportDepositintransit ccrdeposit = null;
            ses.save(ccr);
            Integer ccrid = ccr.getCcrid();
            System.out.println("cashlistsize:" + cashlist.size());

            for (int j = 0; j < checklist.size(); j++) {
                ses.saveOrUpdate(checklist.get(j));
                ccrcheck = new CashcustodianreportChecks(ccr, checklist.get(j));
                ses.save(ccrcheck);
            }

            for (int j = 0; j < fundlist.size(); j++) {
                ses.saveOrUpdate(fundlist.get(j));
            }

            for (int j = 0; j < bankdebitlist.size(); j++) {
                ses.saveOrUpdate(bankdebitlist.get(j));
            }

            for (int j = 0; j < bankcreditlist.size(); j++) {
                ses.saveOrUpdate(bankcreditlist.get(j));
            }

            for (int j = 0; j < cashlist.size(); j++) {
                ses.saveOrUpdate(cashlist.get(j));
            }

            for (int j = 0; j < collectionlist.size(); j++) {
                ccrcoll = new CashcustodianreportCollections(ccrid, collectionlist.get(j).getCollectionid());
                ses.save(ccrcoll);
            }

            for (int j = 0; j < depositlist.size(); j++) {
                ccrdeposit = new CashcustodianreportDepositintransit(ccrid, depositlist.get(j).getCollectionid());
                ses.save(ccrdeposit);
            }

            commit();
            flush();
            return "ok";
        } catch (Exception e) {
            System.out.println("ccrerror:");
            e.printStackTrace();
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public String postCCR(Cashcustodianreport ccr, List<Checks> checklist, List<Cashdisbursement> cashlist, List<Additionalfund> fundlist, List<Bankdebitmemo> bankdebitlist, List<Bankcreditmemo> bankcreditlist, List<Collections> collectionlist, List<Collections> depositlist) throws Exception {
        try {
            Session ses = getSession();
            begin();
            Checks check = null;
            Cashdisbursement cashdisbursement = null;
            Additionalfund addtlfund = null;
            Bankdebitmemo bankdebitmemo = null;
            Bankcreditmemo bankcreditmemo = null;
            CashcustodianreportChecks ccrcheck = null;
            ses.update(ccr);


            for (int j = 0; j < checklist.size(); j++) {
                check = checklist.get(j);
                if ((check.getCheckid() != null) && (check.getBankaccountno() == null) && (check.getCheckamount() == 0) && (check.getCheckdate().equals(""))
                        && (check.getCheckno() == null) && (check.getLoans() == null)) {
                    ses.delete(check);
                } else if (check.getCheckid() != null) {
                    ses.update(check);
                } else if (check.getCheckid() == null) {
                    ses.save(check);
                    ccrcheck = new CashcustodianreportChecks(ccr, check);
                    ses.save(ccrcheck);
                }
            }

            for (int j = 0; j < cashlist.size(); j++) {
                cashdisbursement = cashlist.get(j);
                if ((cashdisbursement.getAmount() == 0) && (cashdisbursement.getDocnum() == 0) && (cashdisbursement.getParticulars().equals(""))
                        && (cashdisbursement.getPayeeFirstname().equals("")) && (cashdisbursement.getPayeeMiddlename().equals(""))
                        && (cashdisbursement.getPayeeLastname().equals("")) && (cashdisbursement.getCashcustodianreport() == null)) {
                    ses.delete(cashdisbursement);
                } else {
                    ses.saveOrUpdate(cashdisbursement);
                }
            }

            for (int j = 0; j < fundlist.size(); j++) {
                addtlfund = fundlist.get(j);
                if ((addtlfund.getAmount() == 0) && (addtlfund.getDocnum() == 0) && (addtlfund.getParticulars().equals(""))
                        && (addtlfund.getPayeeFirstname().equals("")) && (addtlfund.getPayeeMiddlename().equals(""))
                        && (addtlfund.getPayeeLastname().equals("")) && (addtlfund.getCashcustodianreport() == null)) {
                    ses.delete(addtlfund);
                } else {
                    ses.saveOrUpdate(addtlfund);
                }
            }

            for (int j = 0; j < bankdebitlist.size(); j++) {
                bankdebitmemo = bankdebitlist.get(j);
                if ((bankdebitmemo.getParticulars().equals("")) && (bankdebitmemo.getAmount() == 0) && (bankdebitmemo.getCashcustodianreport() == null)) {
                    ses.delete(bankdebitmemo);
                } else {
                    ses.saveOrUpdate(bankdebitmemo);
                }

            }

            for (int j = 0; j < bankcreditlist.size(); j++) {
                bankcreditmemo = bankcreditlist.get(j);
                if ((bankcreditmemo.getParticulars().equals("")) && (bankcreditmemo.getAmount() == 0) && (bankcreditmemo.getCashcustodianreport() == null)) {
                    ses.delete(bankcreditmemo);
                } else {
                    ses.saveOrUpdate(bankcreditmemo);
                }
            }

            commit();
            flush();
            return "ok";
        } catch (Exception e) {
            rollback();
            e.printStackTrace();
            throw e;

        }
    }

    @SuppressWarnings("unchecked")
    public Cashcustodianreport getCCREndBal() throws Exception {
        Cashcustodianreport c = (Cashcustodianreport) getSession().createQuery("from Cashcustodianreport c order by c.ccrdate desc").setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return c;
    }

    @SuppressWarnings("unchecked")
    public Cashcustodianreport getByBranchAndDate(Branches branch, String date) {
        try {
            Cashcustodianreport ccr = null;
            ccr = (Cashcustodianreport) getSession().createQuery("from Cashcustodianreport a where a.ccrdate=:date and a.branches=:branch").setParameter("date", date).setParameter("branch", branch).setMaxResults(1).uniqueResult();
            return ccr;
        } catch (Exception e) {
            System.out.println("ccrdaoerr:" + e.toString());
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public List<CashcustodianreportCollections> getCollPerCCR(int ccrid) throws Exception {
        List<CashcustodianreportCollections> collist = (List<CashcustodianreportCollections>) getSession().createQuery("from CashcustodianreportCollections c where c.ccrid=:ccrid").setParameter("ccrid", ccrid).setCacheable(true).list();
        flush();
        return collist;
    }

    @SuppressWarnings("unchecked")
    public List<CashcustodianreportDepositintransit> getDepositPerCCR(int ccrid) throws Exception {
        List<CashcustodianreportDepositintransit> deplist = (List<CashcustodianreportDepositintransit>) getSession().createQuery("from CashcustodianreportDepositintransit c where c.ccrid=:ccrid").setParameter("ccrid", ccrid).setCacheable(true).list();
        flush();
        return deplist;
    }
}
